package cn.stylefeng.guns.modular.yingbao.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.YingbaoRotationImages;
import cn.stylefeng.guns.modular.yingbao.model.params.YingbaoRotationImagesParam;
import cn.stylefeng.guns.modular.yingbao.service.YingbaoRotationImagesService;
import cn.stylefeng.guns.sys.core.listener.ConfigListener;
import cn.stylefeng.guns.sys.core.util.UeditorUtil;
import cn.stylefeng.guns.sys.modular.system.model.UeditorFileResult;
import cn.stylefeng.guns.sys.modular.system.model.UploadResult;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;


/**
 * 轮播图控制器
 *
 * @author jj
 * @Date 2019-12-05 16:41:38
 */
@Controller
@RequestMapping("/yingbaoRotationImages")
@Api(tags = "轮播图")
public class YingbaoRotationImagesController extends BaseController {

    private String PREFIX = "/yingbaoRotationImages";



    @Autowired
    private YingbaoRotationImagesService yingbaoRotationImagesService;

    /**
     * 跳转到主页面
     *
     * @author jj
     * @Date 2019-12-05
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/yingbaoRotationImages.html";
    }

    /**
     * 新增页面
     *
     * @author jj
     * @Date 2019-12-05
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/yingbaoRotationImages_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jj
     * @Date 2019-12-05
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
        return PREFIX + "/yingbaoRotationImages_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jj
     * @Date 2019-12-05
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(YingbaoRotationImagesParam yingbaoRotationImagesParam) {
        this.yingbaoRotationImagesService.add(yingbaoRotationImagesParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jj
     * @Date 2019-12-05
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(YingbaoRotationImagesParam yingbaoRotationImagesParam) {
        this.yingbaoRotationImagesService.update(yingbaoRotationImagesParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jj
     * @Date 2019-12-05
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(YingbaoRotationImagesParam yingbaoRotationImagesParam) {
        this.yingbaoRotationImagesService.delete(yingbaoRotationImagesParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jj
     * @Date 2019-12-05
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(YingbaoRotationImagesParam yingbaoRotationImagesParam, Model model ) {
        System.out.println(yingbaoRotationImagesParam);
        YingbaoRotationImages detail = this.yingbaoRotationImagesService.getById(yingbaoRotationImagesParam.getId());
        System.out.println(ConfigListener.getConf().get("contextPath")+detail.getImageUrl());
        model.addAttribute("avatar", ConfigListener.getConf().get("contextPath")+detail.getImageUrl());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jj
     * @Date 2019-12-05
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(YingbaoRotationImagesParam yingbaoRotationImagesParam) {
        return this.yingbaoRotationImagesService.findPageBySpec(yingbaoRotationImagesParam);
    }

    /**
     * 查询列表(对外接口)
     *
     * @author jj
     * @Date 2019-12-05
     */
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/qdlist",method = RequestMethod.POST)
    @ApiOperation("获取轮播图")
    @ApiResponse(code=400,message="请求错误")
    public ResponseData list() {
        List<YingbaoRotationImages> pageBySpec = yingbaoRotationImagesService.findPageBySpec();
        return CollectionUtils.isEmpty(pageBySpec) ? ResponseData.error(400,"错误！"):ResponseData.success(pageBySpec);
    }


    /**
     * layui上传组件 通用文件上传接口
     *
     * @author fengshuonan
     * @Date 2019-2-23 10:48:29
     */
    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public ResponseData layuiUpload(@RequestPart("file") MultipartFile file) {

        UeditorFileResult uploadResult = UeditorUtil.uploadFile(file, UeditorUtil.FileType.IMG);
        String fileSavePath = uploadResult.getUrl();
        HashMap<String, Object> map = new HashMap<>();
        map.put("fileSavePath", fileSavePath);

        return ResponseData.success(0, "上传成功", map);
    }

}


